package com.mrent.web.controller.front;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import com.mrent.entity.Alipay;
import com.mrent.entity.Order;
import com.mrent.service.OrderService;
import com.mrent.service.Page;
import com.mrent.util.ConfigHelper;
import com.mrent.util.Constance;
import com.mrent.util.DateUtil;
import com.mrent.util.FormDateLoader;
import com.mrent.util.RenderUtil;
import com.mrent.util.Utils;

@Controller
public class OrderController {

	@Autowired
	private OrderService orderService;

	/**
	 * 跳转申请页.
	 * 
	 * @param model
	 * @return
	 */
	@RequestMapping("/order/reg")
	public String reg(ModelMap model) {

		/**
		 * 日期相关.
		 * 
		 */
		model.addAttribute("years", FormDateLoader.getSelectYear());
		model.addAttribute("months", FormDateLoader.getSelectMonth());
		model.addAttribute("days", FormDateLoader.getSelectDay());

		return "front/order/reg";
	}

	/**
	 * 订单保存.
	 * 
	 * @param order
	 * @param isDefaultPrice
	 * @param response
	 * @return
	 */
	@RequestMapping("/order/save")
	public String save(Order order, Integer isDefaultPrice,
			HttpServletResponse response) {
		
		List<Order> samelist = orderService.listOrderBySameDate(order.getOrderRequestYear(), order.getOrderRequestMonth(), order.getOrderRequestDay(),order.getOrderEndYear(),order.getOrderEndMonth(),order.getOrderEndDay());
		List<Order> list = orderService.listOrderByDate(order.getOrderRequestYear(), order.getOrderRequestMonth(), order.getOrderRequestDay(),order.getOrderEndYear(),order.getOrderEndMonth(),order.getOrderEndDay());
		System.out.println("list size:" + list.size());
		order.setOrderDate(new Date());
		order.setOrderNo(Utils.generDataNo());
		Map<String, Object> json = new HashMap<String, Object>();
		//判断是否有相同开店日期，与相同到期日期的订单.
		if(samelist.size() > 0){
			json.put("result", "Faile");
			json.put("ErrorCode", "-6");
			RenderUtil.renderJson(json, response);
			return null;
		}
		// 判断是否默认单价，并判断竞价是否小于默认单价.
		if (isDefaultPrice == 1) {
			order.setOrderPrice(Constance.DEFAULT_DAYS_PRICE);
		} else {
			if (order.getOrderPrice().compareTo(Constance.DEFAULT_DAYS_PRICE) < 0) {
				json.put("result", "Faile");
				json.put("ErrorCode", "-1");
				json.put("value", Constance.DEFAULT_DAYS_PRICE);
				RenderUtil.renderJson(json, response);
				return null;
			}
		}

		// 计算天数.
		long days = countDays(order);
		if (days <= 0) {
			json.put("result", "Faile");
			json.put("ErrorCode", "-2");
			RenderUtil.renderJson(json, response);
			return null;
		}

		order.setOrderRequestDayNum(days);
		if (list.size() > 0) {// 存在.
			for (Order orderdb : list) {
				if (orderdb.getOrderState() < Constance.ORDER_PAYED) {// 未付款.
					if (order.getOrderPrice().compareTo(orderdb.getOrderPrice()) < 0) {// 比存在的订单单价小,返回提示不成功
						json.put("result", "Faile");
						json.put("ErrorCode", "-3");
						RenderUtil.renderJson(json, response);
						return null;
					} else if (order.getOrderPrice().compareTo(orderdb.getOrderPrice()) == 0) {// 与存在的订单单价相等,判断天数.
						if (order.getOrderRequestDayNum() >= orderdb.getOrderRequestDayNum()) {// 天数比存在的订单大.返回提示不成功
							json.put("result", "Faile");
							json.put("ErrorCode", "-4");
							RenderUtil.renderJson(json, response);
							return null;
						} else {// 天数比存在的订单小,保存新订单为确认，旧订单为不确认
							order.setOrderState(Constance.ORDER_CONFIRM);
							orderService.saveOrder(order);
							orderdb.setOrderState(Constance.ORDER_NOT_CONFIRM);
							orderService.updateOrder(orderdb);

							json.put("result", "Success");
							RenderUtil.renderJson(json, response);
							return null;
						}
					} else {// 比存在的订单单价大,保存新订单为确认，旧订单为不确认.
						order.setOrderState(Constance.ORDER_CONFIRM);
						orderService.saveOrder(order);
						orderdb.setOrderState(Constance.ORDER_NOT_CONFIRM);
						orderService.updateOrder(orderdb);

						json.put("result", "Success");
						RenderUtil.renderJson(json, response);
						return null;
					}
				} else {// 已付款,返回提示不成功.
					json.put("result", "Faile");
					json.put("ErrorCode", "-5");
					RenderUtil.renderJson(json, response);
					return null;
				}
			}
		} else {// 不存在.
			order.setOrderState(Constance.ORDER_CONFIRM);
			orderService.saveOrder(order);
			json.put("result", "Success");
			RenderUtil.renderJson(json, response);
		}

		return null;
	}

	

	/**
	 * 计算天数.
	 * 
	 * @param order
	 * @return
	 */
	private long countDays(Order order) {
		long days = 1;
		days = DateUtil.getDaysBetween(order.getOrderEndYear(), order.getOrderEndMonth(), order.getOrderEndDay(), order.getOrderRequestYear(), order.getOrderRequestMonth(), order.getOrderRequestDay());
		return days;
	}

}
